<?php
namespace Home\Controller;

class WeightController extends CommonController{
    
    //体重列表
    public function index(){
        //体重文案
        $this->assign('words',M('Words')->field('weight')->find(1));
        
        $p = I('request.p') ? I('request.p') : 1;
        $list = M('Weight')->where(array('uid'=>$this->uid))->order('id desc')->page($p.',10')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time'] = fmt_date($v['create_time']);
        }
        
        if($p == 1){
            $this->assign('list',$list);
            $this->display();
        }else{
            if(empty($list)){
                echo json_encode('err');die;
            }else{
                echo json_encode($list);die;
            }
        }
    }
    
    //记录体重
    public function edit(){
        $Weight = D('Weight');
        $create_date = date('Y-m-d');
        if(IS_POST){
            //每日记录不超过3次
            $count = $Weight->where(array('uid'=>$this->uid,'create_date'=>$create_date))->count();
            if($count >= 3) $this->ajaxReturn('over');
            //保存体重
            $res = $Weight->add(array(
                'uid'=>$this->uid,
                'vol'=>I('post.vol'),'create_date'=>$create_date
            ));
            
            D('Score')->addWeight($this->uid);//积分-增加
            
            $res ? $this->ajaxReturn('suc') : $this->ajaxReturn('err');
        }else{
            //今日记录
            $list = $Weight->where(array('uid'=>$this->uid,'create_date'=>$create_date))->order('id asc')->limit(6)->select();
            $count = $Weight->where(array('uid'=>$this->uid,'create_date'=>$create_date))->count();
            $this->assign('list',$list);
            $this->assign('now_date',$create_date);
            $this->assign('current_times',$count+1);
            $this->display();
        }
    }
    
    public function del(){
        $Weight = M('Weight');
        $id = I('post.id');
        $row = $Weight->find($id);
        if($row['uid'] == $this->uid){
            $res = $Weight->delete($id);
            $res ? die('suc') : die('err');
        }else{
            die('err');
        }
    }
    
    //体重图表
    public function chart(){
        if($pid = I('get.pid')){
            $this->uid = I('get.pid');
        }else{
            $pid = '';
        }
        $this->assign('pid',$pid);
        //用户信息
        $this->assign('userInfo',M('User')->find($this->uid));
        //体重文案
        $this->assign('words',M('Words')->field('weight')->find(1));
        
        //体重柱状图
        $current_time = time();
        $list = array();$listx = array();$listy = array();
        $create_date = date('Y-m-d',$current_time);
        $current_n = date('N',$current_time);//今天周几
        
        //生成图表纵横轴数据
        if(I('get.type') == 'week'){//本周
            $dateArr = getNowWeekDate();
            $list = M('Weight')->where(array('uid'=>$this->uid,'create_date'=>array('in',$dateArr)))->order('id asc')->select();
            foreach($list as $v){
                $listx[] = substr($v['create_date'],5,5);
                $listy[] = $v['vol'];
            }
        }else{//本天
            $list = M('Weight')->where(array('uid'=>$this->uid,'create_date'=>$create_date))->order('id asc')->select();
            foreach($list as $v){
                $listx[] = substr($v['create_time'],11,5);
                $listy[] = $v['vol'];
            }
        }

        $this->assign('xStr',"'".implode("','",$listx)."'");
        $this->assign('yStr',implode(",",$listy));
        $this->display();
    }
    
}